package com.hgsoft.pubsv.component.apifilter.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartResolver;

import com.hgsoft.pubsv.component.apifilter.filter.EncryptionFilter;
import com.hgsoft.pubsv.component.apifilter.init.ApiSecuritData;
import com.hgsoft.pubsv.component.apifilter.propertie.SecurityPropertie;
import com.hgsoft.pubsv.component.base.redis.RedisScriptUtil;
import com.hgsoft.pubsv.component.base.token.IOAuthCacheTokenService;

/**
 * 加载api安全注解
 * 
 * @author saint
 *
 */
public class SecurityConfig {
	@Autowired
	private SecurityPropertie securityPropertie;
	@Autowired
	private RedisScriptUtil redisScriptUtil;
	@Autowired
	private MultipartResolver multipartResolver;
	@Autowired
	private IOAuthCacheTokenService authCacheTokenService;

	/**
	 * 不要用泛型注册Filter,泛型在Spring Boot 2.x版本中才有
	 * 可自定义注解启动
	 *
	 * @return 过滤器
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Bean
	public FilterRegistrationBean filterRegistration() {
		FilterRegistrationBean registration = new FilterRegistrationBean();
		registration.setFilter(
				new EncryptionFilter(securityPropertie, redisScriptUtil, multipartResolver, authCacheTokenService));
		registration.addUrlPatterns(securityPropertie.getUrlPatterns());
		registration.setName("EncryptionFilter");
		registration.setOrder(securityPropertie.getOrder());
		return registration;
	}

	@Bean
	public ApiSecuritData apiEncryptDataInit() {
		return new ApiSecuritData();
	}
}
